﻿using System;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using easyccms.BusinessLayer;
using easyccms.BusinessLayer.Repositories;
using easyccms.Nhibernate;
using easyccms.Nhibernate.Repositories;

namespace easyccms.WebUI.Infrastructure
{
    /// <summary>
    /// 
    /// </summary>
  public class WindsorConfigure
  {
    /// <summary>
    /// The Windsor container to initialise.
    /// </summary>
    private static IWindsorContainer _windsorContainer;

    public static IWindsorContainer GetConfiguration()
    {
      _windsorContainer = new WindsorContainer();
      Configure();
      return _windsorContainer;
    }

    /// <summary>
    /// Configures this instance.
    /// </summary>
    private static void Configure()
    {
      //AddConfiguration<Castle_MVC2.Interfaces.IPavel, Pavel>();


        _windsorContainer.Register(
            Component.For<IUnitOfWork>().ImplementedBy(typeof(UnitOfWork)).LifeStyle.PerWebRequest);
        _windsorContainer.Register(
            Component.For<IArticleRepository>().ImplementedBy(typeof(ArticleRepository)).LifeStyle.PerWebRequest);
        
        _windsorContainer.Register(Component.For<IMenuItemRepository>().ImplementedBy(typeof(MenuItemRepository)).LifeStyle.PerWebRequest);
            _windsorContainer.Register(Component.For<IMenuRepository>().ImplementedBy(typeof(MenuRepository)).LifeStyle.PerWebRequest);
            _windsorContainer.Register(Component.For<IUserGroupRepository>().ImplementedBy(typeof(UserGroupRepository)).LifeStyle.PerWebRequest);
            _windsorContainer.Register(Component.For<IUserOnlineRepository>().ImplementedBy(typeof(UserOnlineRepository)).LifeStyle.PerWebRequest);
            _windsorContainer.Register(Component.For<IUserProfileRepository>().ImplementedBy(typeof(UserProfileRepository)).LifeStyle.PerWebRequest);
            _windsorContainer.Register(Component.For<ISiteUserRepository>().ImplementedBy(typeof(SiteUserRepository)).LifeStyle.PerWebRequest);




            

      var controllerTypes =
            from t in Assembly.GetCallingAssembly().GetTypes()
            where typeof(IController).IsAssignableFrom(t)
            select t;
      foreach (Type t in controllerTypes)
      {
          //register every controller by its full name and so this allows users to 
          //have multiple controller with the same name as long they are in different locations.
          _windsorContainer.Register(Component.For(t).Named(t.FullName).LifeStyle.Transient);
      }
    }
  }
}